package com.bmps.springcloud.insuredmicroservice.controller.trial;

import com.alibaba.fastjson.JSONObject;
import com.bmps.springcloud.common.response.TaskResponseModel;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.TrialConstant;
import com.bmps.springcloud.insuredmicroservice.controller.accept.InsuranceAcceptSupport;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.AcceptResponseDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.ResponseProductInfoModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.response.ResultModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.AcceptRequestDataModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestHeaderModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.request.TrailRequestDateModel;
import com.bmps.springcloud.insuredmicroservice.service.underwrite.AcceptService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * 保费试算类
 * @author :  zlc
 * @date : 2019/8/22 11:17
 **/
@RestController
@RequestMapping(value="/insured")
public class InsuranceTrialController {

    private static Logger logger = LoggerFactory.getLogger(InsuranceTrialController.class);

    private final AcceptService acceptService;

    public InsuranceTrialController(AcceptService acceptService) {
        this.acceptService = acceptService;
    }

    @PostMapping(value = "/trialpolicy", headers = "Content-Type=application/json;charset=UTF-8")
    public AcceptResponseDataModel trialPolicy(@Valid @RequestBody TrailRequestDateModel trailRequestDateModel,
                                               BindingResult bindingResult){
        logger.debug("【参数】" +trailRequestDateModel.toString());
        //校验参数是否符合设定的规则，如果不符合，返回

        if (bindingResult.hasErrors()) {
            RequestHeaderModel requestHeaderModel = new RequestHeaderModel();
            BeanUtils.copyProperties(trailRequestDateModel.getHeader(),requestHeaderModel);
            return InsuranceAcceptSupport.bindingErrors(requestHeaderModel, bindingResult);
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(0);
        ResultModel resultModel = new ResultModel();
        String jsonString = JSONObject.toJSONString(trailRequestDateModel);
        AcceptRequestDataModel acceptRequestDataModel = JSONObject.parseObject(jsonString ,AcceptRequestDataModel.class);
        logger.info("转换后的Accept对象"+acceptRequestDataModel.toString());
        List<ProductModel> productModelList = acceptRequestDataModel.getRequest().getProductInfo().getProducts().getProduct();

        for (ProductModel productModel : productModelList) {
            TaskResponseModel taskResponseModel = acceptService.checkPremiumTrialCalculation(
                    null,productModel,acceptRequestDataModel);
            logger.info("【单个结果】"+taskResponseModel.toString());
            if (StringUtils.equals(taskResponseModel.getResultCode(), AcceptConstant.RESULT_SUCCESS_CODE)) {
                bigDecimal = bigDecimal.add(taskResponseModel.getBigDecimal());
            } else {
                resultModel.setResultCode(TrialConstant.TRIAL_RESULT_FIAL_CODE);
                resultModel.setResultMessage(TrialConstant.TRIAL_RESULT_FIAL_MESSAGE);
                return AcceptUtil.getResponseResult(acceptRequestDataModel.getHeader(), resultModel, null);
            }
        }
        
        ResponseProductInfoModel responseProductInfoModel = new ResponseProductInfoModel();
        // responseProductInfoModel.setPayPrem(TrialUtil.formatScaleNumber("#0.00",bigDecimal.doubleValue()));
        // 结果四舍五入
        responseProductInfoModel.setPayPrem(TrialUtil.roundHalfUp(bigDecimal.doubleValue()));
        resultModel.setResultCode(TrialConstant.TRIAL_RESULT_SUCCESS_CODE);
        resultModel.setResultMessage(TrialConstant.TRIAL_RESULT_SUCCESS_MESSAGE);
        return AcceptUtil.getResponseResult(acceptRequestDataModel.getHeader(), resultModel, responseProductInfoModel);
    }
}
